package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.MyMessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Role;
import com.itheima.pojo.User;
import com.itheima.service.UserService;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {

    @Reference
    private UserService userService;

    @RequestMapping("/deleteUser")
    public Result deleteUser(Integer id){
        try {
            userService.deleteUser(id);
            return new Result(true, MyMessageConstant.USER_DELETE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            if (e.getMessage().contains("foreign key constraint")) {
                List<Role> roleList = userService.findRoleByUserId(id);
                StringBuffer sb = new StringBuffer();
                if (roleList!=null){
                    for (Role role : roleList) {
                        sb.append(role.getName()).append(",");
                    }
                    return new Result(false, "该用户已关联"+sb.toString().substring(0,(sb.length()-1))+"角色,请先解除绑定,再进行删除操作");
                }
            }
            return new Result(false, MyMessageConstant.USER_DELETE_FAIL);
        }
    }

    /**
     * 编辑
     * @param user
     * @param roleIds
     * @return
     */
    @RequestMapping("/edit")
    public Result edit(@RequestBody User user,Integer[] roleIds){
        try {
            userService.edit(user, roleIds);
            return new Result(true,MyMessageConstant.USER_EDIT_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MyMessageConstant.USER_EDIT_FAIL);
        }
    }

    /**
     * 根据用户id,查询用用户的角色
     * @param id
     * @return
     */
    @RequestMapping("/findRoleIdsByUserId")
    public Result findRoleIdsByUserId(Integer id) {
        try {
            List<Integer> roleIds = userService.findRoleIdsByUserId(id);
            return new Result(true, MyMessageConstant.ROLE_QUERY_SUCCESS, roleIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MyMessageConstant.ROLE_QUERY_FAIL);
        }
    }

    /**
     * 根据id查询用户
     *
     * @param id
     * @return
     */
    @RequestMapping("/findUserById")
    public Result findUserById(Integer id) {
        try {
            User user = userService.findUserById(id);
            return new Result(true, MyMessageConstant.USER_QUERY_SUCCESS, user);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MyMessageConstant.USER_QUERY_FAIL);
        }
    }

    /**
     * 新增后台用户
     *
     * @param user
     * @param user
     * @param roleIds
     * @return
     */
    @RequestMapping("/addUser")
    public Result addUser(@RequestBody User user, @RequestParam("roleIds") Integer[] roleIds) {
        System.out.println(user);
        try {
            userService.addUser(user, roleIds);
            return new Result(true, MyMessageConstant.USER_ADD_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MyMessageConstant.USER_ADD_FAIL);
        }
    }

    /**
     * 查询所有后台用户的信息
     *
     * @return
     */
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        PageResult pageResult = userService.findPage(queryPageBean);
        return pageResult;
    }

    //获取当前登录用户的用户名
    @RequestMapping("/getUsername")
    public Result getUsername() throws Exception {
        try {
            User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            return new Result(true, MessageConstant.GET_USERNAME_SUCCESS, user.getUsername());
        } catch (Exception e) {
            return new Result(false, MessageConstant.GET_USERNAME_FAIL);
        }
    }
}
